<?php

// +----------------------------------------------------------------------
// |目录权限服务层
// +----------------------------------------------------------------------

namespace app\common\system;

use app\common\Mode\MenuModel;
use think\facade\Cache;


/**
 * 目录权限
 */
class MenuService
{

    /**
     * 获取的菜单的数据
     * @param array $param 前台传过来的参数  
     * @return array
     */
    public static function  getlist($param){
        $MenuModel = new MenuModel(); 
        $pk = $MenuModel->getPk();      
        $params= $param['params'];
        $field = $pk . ',title,pid,icon,url,sort,ismenu';   
        $where[] = ['status', '=', 1];    
        if($params['title']){
            $where[] = ['title', 'like', '%'. $params['title']. '%'];
        }
        if($params['ismenu']==2){
            $where[] = ['ismenu', '=', 0];
        }else  if($params['ismenu']==1){
            $where[] = ['ismenu', '=', 1];
        } 
       
        $menu_list = $MenuModel->field($field)->where($where)->order('sort asc,id asc')->paginate(['list_rows'=>$params['pagesize'],'page'=>$params['pagenum']])->toArray(); 
      
        if($menu_list['data']){
            foreach ($menu_list['data'] as $key => $value) {
                  // 找到 '/index/' 的位置
             
                if (strpos($value['url'], '/index/') !== false) {
                    // 提取 '/index/' 部分
                    $indexPosition = strpos($value['url'], '/index/');
                    $menu_list['data'] [$key]['prefix'] = substr($value['url'], 0, $indexPosition + strlen('/index/'));
                    // 提取剩余部分
                    $menu_list['data'] [$key]['urls']= substr($value['url'], $indexPosition + strlen('/index/'));
       
                } elseif (strpos($value['url'], '/admin/')!== false) {
                    // 提取 '/index/' 部分
                    $indexPosition = strpos($value['url'], '/admin/');
                    $menu_list['data'] [$key]['prefix'] = substr($value['url'], 0, $indexPosition + strlen('/admin/'));
                    // 提取剩余部分
                    $menu_list['data'] [$key]['urls']= substr($value['url'], $indexPosition + strlen('/admin/'));
                }
            }
        } 
        $data['total']=$menu_list['total'];
        if($params['ismenu']==2){
            //等于2  说明前台选择不是菜单，不是菜单则不显示树状
            $data['list']=$menu_list['data'];
        }else{
            $data['list']=buildTree($menu_list['data']);
        }
      
        return $data;
     }

     /**
     * 修改菜单是否为菜单
     * @param array $param 前台传过来的参数  
     * @return array
     */
     public static function  update_ismenus($param){ 
            $MenuModel = new MenuModel();
            $params= $param['params'];
            $idarr=self::getAllChildrenIds($params['id']);
            $idarr[]=$params['id'];
            $result =  $MenuModel->where('id','in',$idarr)->update(['ismenu' => $params['ismenu']]);
            if($result){
                return true;
            }else{
                error('修改失败');
            }
     
    }

      /**
     * 获取所有菜单列表
 
     * @return array
     */       
    public static function list(){
        $MenuModel = new MenuModel(); 
        $pk = $MenuModel->getPk();         
        $field = $pk . ',title ,laval,pid';   
        $where[] = ['status', '=', 1];    
        $menu_list = $MenuModel->field($field)->where($where)->order('sort asc,id asc')->select()->toArray();       
        return buildTree($menu_list);

    }

     /**
     * 修改菜单是否为菜单
     * @param array $param 前台传过来的参数  
     * @return array
     */   
    public static function create($param){
        $MenuModel = new MenuModel();
        $params= $param['params'];
        if(isset($params['prefix']) && isset($params['urls'])){
            $url = $params['prefix'] . $params['urls'];
        }else{
            $url = '';
        }
        $MenuModel->data([
            
            'title' => $params['title'],
            'pid' => $params['pid'],        
            'url' => $url ,
            'sort' => $params['sort'],
            'ismenu' => $params['ismenu'],
        ]);
        $result =  $MenuModel->save();
        if($result){
            return true;
        }else{
            error('添加失败');
        }
    
    }

  


        /**
     * 递归方法 获取所有子级ID
     * @param $parentId 菜单父级ID
     * @return array
     */
   public static  function getAllChildrenIds($parentId) {
        $MenuModel = new MenuModel(); 
        // 使用where方法来筛选父级ID
        $children =$MenuModel->where(array('pid' => $parentId))->select();
        
        // 初始化子级ID数组
        $childrenIds = [];
        foreach ($children as $child) {
            $childrenIds[] = $child['id'];
        }
        
        // 递归获取所有子级ID
        foreach ($children as $child) {
            $childrenIds = array_merge($childrenIds, self::getAllChildrenIds($child['id']));
        }
        
        return $childrenIds;
    }


 
}